/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinotrack.struts;

import com.dinotrack.domain.Persona;
import com.dinotrack.domain.PersonaSQL;
import com.dinotrack.domain.Usuario;
import com.dinotrack.domain.UsuarioSQL;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author alquad
 */
public class EditAccountAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession(); 
        boolean load = Boolean.valueOf(String.valueOf(session.getAttribute("LOAD_DATA")));

        System.out.println("########################## EN ACTION");
        
        if (load) {
            System.out.println("########################## EN ACTION AJAX");
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            String userName = String.valueOf(session.getAttribute("USER"));
            
            UsuarioSQL userSQL = new UsuarioSQL(); 
            Usuario user= userSQL.getUsuario(userName);
            
            PersonaSQL personSQL = new PersonaSQL(); 
            Persona person = personSQL.getPersona(userName);
            
            try {
                PrintWriter writer = response.getWriter();
                writer.print("<xml><user><![CDATA["+user.getUsuario()+"]]></user>"
                        + "<password><![CDATA["+user.getContrasenia()+"]]></password>"
                        + "<name><![CDATA["+person.getNombre()+"]]></name>"
                        + "<lastName><![CDATA["+ person.getApellido()+"]]></lastName>"
                        + "<email><![CDATA["+person.getEmail()+"]]></email>"
                        + "</xml>");
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            return null;

        }

        return mapping.findForward(SUCCESS);
    }
}
